

#include "stdafx.h"
#include "pf_dimaligned.h"

#include "pf_graphic.h"
#include "pf_units.h"
#include "pf_constructionline.h"


/**
 * Constructor.
 *
 * @para parent Parent Entity Container.
 * @para d Common dimension geometrical data.
 * @para ed Extended geometrical data for aligned dimension.
 */
PF_DimAligned::PF_DimAligned(PF_Container* parent,
                             const PF_DimensionAttribute& d,
                             const PF_DimAlignedAttribute& ed)
        : PF_Dimension(parent, d), edata(ed) {

    calculateBorders();
}



/**
 * Sets a new text. The entities representing the 
 * text are updated.
 */
//void PF_DimAligned::setText(const QString& t) {
//    data.text = t;
//    update();
//}


PF_VectorSolutions PF_DimAligned::getRefPoints() {
	PF_VectorSolutions ret(edata.extensionPoint1, edata.extensionPoint2,
						data.definitionPoint, data.middleOfText);
	return ret;
}



/**
 * @return Automatically creted label for the default 
 * measurement of this dimension.
 */
QString PF_DimAligned::getMeasuredLabel() {
    double dist = edata.extensionPoint1.distanceTo(edata.extensionPoint2);

	PF_Graphic* graphic = getGraphic();

    QString ret;
	if (graphic!=NULL) {
		ret = PF_Units::formatLinear(dist, graphic->getUnit(), 
			graphic->getLinearFormat(), graphic->getLinearPrecision());
	}
	else {
        ret = QString("%1").arg(dist);
	}
    return ret;
}



/**
 * Updates the sub entities of this dimension. Called when the 
 * text or the position, alignment, .. changes.
 *
 * @param autoText Automatically reposition the text label
 */
void PF_DimAligned::update(bool autoText) {

    PF_DEBUG->print("PF_DimAligned::update");

    clear();

	if (isUndone()) {
		return;
	}

    // distance from entities (DIMEXO)
    double dimexo = getExtensionLineOffset();
    // definition line definition (DIMEXE)
    double dimexe = getExtensionLineExtension();
    // text height (DIMTXT)
    //double dimtxt = getTextHeight();
    // text distance to line (DIMGAP)
    //double dimgap = getDimensionLineGap();

    // Angle from extension endpoints towards dimension line
    double extAngle = edata.extensionPoint2.angleTo(data.definitionPoint);
    // extension lines length
    double extLength = edata.extensionPoint2.distanceTo(data.definitionPoint);

    PF_Vector v1, v2, e1;
    PF_LineAttribute ld;
    PF_Line* line;

    v1.setPolar(dimexo, extAngle);
    v2.setPolar(dimexe, extAngle);
    e1.setPolar(1.0, extAngle);

    // Extension line 1:
    ld = PF_LineAttribute(edata.extensionPoint1 + v1,
                     edata.extensionPoint1 + e1*extLength + v2);
    line = new PF_Line(this, ld);
    //line->setLayerToActive();
    //line->setPenToActive();
    line->setPen(PF_Pen(LX_Define::FlagInvalid));
    line->setLayer(NULL);
    addEntity(line);

    // Extension line 2:
    ld = PF_LineAttribute(edata.extensionPoint2 + v1,
                     edata.extensionPoint2 + e1*extLength + v2);
    line = new PF_Line(this, ld);
    //line->setLayerToActive();
    //line->setPenToActive();
    line->setPen(PF_Pen(LX_Define::FlagInvalid));
    line->setLayer(NULL);
    addEntity(line);

    // Dimension line:
    updateCreateDimensionLine(edata.extensionPoint1 + e1*extLength,
                              edata.extensionPoint2 + e1*extLength,
							  true, true, autoText);

    calculateBorders();
}



bool PF_DimAligned::hasEndpointsWithinWindow(PF_Vector v1, PF_Vector v2) {
	return (edata.extensionPoint1.isInWindow(v1, v2) ||
	        edata.extensionPoint2.isInWindow(v1, v2));
}


void PF_DimAligned::move(PF_Vector offset) {
    PF_Dimension::move(offset);

    edata.extensionPoint1.move(offset);
    edata.extensionPoint2.move(offset);
    update();
}



void PF_DimAligned::rotate(PF_Vector center, double angle) {
    PF_Dimension::rotate(center, angle);

    edata.extensionPoint1.rotate(center, angle);
    edata.extensionPoint2.rotate(center, angle);
    update();
}



void PF_DimAligned::scale(PF_Vector center, PF_Vector factor) {
    PF_Dimension::scale(center, factor);

    edata.extensionPoint1.scale(center, factor);
    edata.extensionPoint2.scale(center, factor);
    update();
}


void PF_DimAligned::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
    PF_Dimension::mirror(axisPoint1, axisPoint2);

    edata.extensionPoint1.mirror(axisPoint1, axisPoint2);
    edata.extensionPoint2.mirror(axisPoint1, axisPoint2);
    update();
}


void PF_DimAligned::stretch(PF_Vector firstCorner,
                        PF_Vector secondCorner,
                        PF_Vector offset) {
						
    //e->calculateBorders();
    if (getMin().isInWindow(firstCorner, secondCorner) &&
            getMax().isInWindow(firstCorner, secondCorner)) {

        move(offset);
    }
	else {	
		//PF_Vector v = data.definitionPoint - edata.extensionPoint2;
		double len = edata.extensionPoint2.distanceTo(data.definitionPoint);
		double ang1 = edata.extensionPoint1.angleTo(edata.extensionPoint2) 
		             + M_PI/2;
	
    	if (edata.extensionPoint1.isInWindow(firstCorner,
                                      secondCorner)) {
        	edata.extensionPoint1.move(offset);
    	} 
    	if (edata.extensionPoint2.isInWindow(firstCorner,
                                      secondCorner)) {
        	edata.extensionPoint2.move(offset);
    	} 
		
		double ang2 = edata.extensionPoint1.angleTo(edata.extensionPoint2) 
		             + M_PI/2;

		double diff = PF_Math::getAngleDifference(ang1, ang2);
		if (diff>M_PI) {
			diff-=2*M_PI;
		}

		if (fabs(diff)>M_PI/2) {
			ang2 = PF_Math::correctAngle(ang2+M_PI);
		}

		PF_Vector v;
		v.setPolar(len, ang2);
		data.definitionPoint = edata.extensionPoint2 + v;
	}
	update(true);
}



void PF_DimAligned::moveRef(const PF_Vector& ref, const PF_Vector& offset) {

    if (ref.distanceTo(data.definitionPoint)<1.0e-4) {
		PF_ConstructionLine l(NULL, 
			PF_ConstructionLineAttribute(edata.extensionPoint1,
				edata.extensionPoint2));
		double d = l.getDistanceToPoint(data.definitionPoint+offset);
		double a = edata.extensionPoint2.angleTo(data.definitionPoint);
		double ad = PF_Math::getAngleDifference(a, 
			edata.extensionPoint2.angleTo(data.definitionPoint+offset));

		if (fabs(ad)>M_PI/2.0 && fabs(ad)<3.0/2.0*M_PI) {
			a = PF_Math::correctAngle(a+M_PI);
		}
		
		PF_Vector v;
		v.setPolar(d, a);
        data.definitionPoint = edata.extensionPoint2 + v;
		update(true);
    }
	else if (ref.distanceTo(data.middleOfText)<1.0e-4) {
        data.middleOfText.move(offset);
		update(false);
    }
	else if (ref.distanceTo(edata.extensionPoint1)<1.0e-4) {
		double a1 = edata.extensionPoint2.angleTo(edata.extensionPoint1);
		double a2 = edata.extensionPoint2.angleTo(edata.extensionPoint1+offset);
		double d1 = edata.extensionPoint2.distanceTo(edata.extensionPoint1);
		double d2 = edata.extensionPoint2.distanceTo(edata.extensionPoint1+offset);
		rotate(edata.extensionPoint2, a2-a1);
		if (fabs(d1)>1.0e-4) {
			scale(edata.extensionPoint2, PF_Vector(d2/d1, d2/d1));
		}
		update(true);
    }
	else if (ref.distanceTo(edata.extensionPoint2)<1.0e-4) {
		double a1 = edata.extensionPoint1.angleTo(edata.extensionPoint2);
		double a2 = edata.extensionPoint1.angleTo(edata.extensionPoint2+offset);
		double d1 = edata.extensionPoint1.distanceTo(edata.extensionPoint2);
		double d2 = edata.extensionPoint1.distanceTo(edata.extensionPoint2+offset);
		rotate(edata.extensionPoint1, a2-a1);
		if (fabs(d1)>1.0e-4) {
			scale(edata.extensionPoint1, PF_Vector(d2/d1, d2/d1));
		}
		update(true);
    }
}



/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_DimAligned& d) {
    os << " DimAligned: " << d.getData() << "\n" << d.getEData() << "\n";
    return os;
}


